/*
 * @ProjectName: 编程学习
 * @Copyright:   2021 HangZhou Yiyuery Dev, Ltd. All Right Reserved.
 * @address:     微信搜索公众号「架构探险之道」获取更多资源。
 * @date:        2021/1/13 10:03 下午
 * @blog:         https://yiyuery.blog.csdn.net/
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package pub.dsb.example.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @date 2021/1/13 10:03 下午
 * @sine 1.0
 */
public class NioFileChannelTest {
    public static void main(String[] args) throws IOException {
        long t1 = System.currentTimeMillis();
        FileInputStream fileInputStream = new FileInputStream("/Users/xiazhaoyang/Documents/Books/计算机/网络通信/TCP-IP详解卷1：协议.pdf");
        FileOutputStream fileOutputStream = new FileOutputStream("/Users/xiazhaoyang/Ashe/workspace/architectrue-adventure/netty-examples/netty-chapter-1/out/production/COPY.pdf");
        // 获取通道
        FileChannel inChannel = fileInputStream.getChannel();
        FileChannel outChannel = fileOutputStream.getChannel();
        // 创建并使用非直接缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (inChannel.read(buffer) != -1) {
            buffer.flip();
            outChannel.write(buffer);
            buffer.clear();
        }
        System.out.println("复制耗时：" + (System.currentTimeMillis() - t1));
        File file = new File("/Users/xiazhaoyang/Ashe/workspace/architectrue-adventure/netty-examples/netty-chapter-1/out/production/COPY.pdf");
        System.out.println("文件大小:"+file.length());
    }
}
